package 中等;

public class Z字形变换 {
    public static void main(String[] args) {
        String s = "A";//"PAYPALISHIRING"
        System.out.println(convert(s, 1));
    }

    public static String convert(String s, int numRows) {
        if (numRows==1){
            return s;
        }
        int length = s.length();
        int l = 2 * numRows - 2;
        int kuan = (length / l + 1) * (numRows - 1);
        char[][] arr =new char[numRows][kuan];
        for (int i = 0; i < length; i++) {
            int j = i / l;
            int k = i % l;
            if (k<numRows){
                arr[k][j*(numRows-1)] = s.charAt(i);
            }else {
                int l1 = k - numRows+1;
                arr[numRows-1-l1][j*(numRows-1)+l1] = s.charAt(i);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                char c = arr[i][j];
                if (c!=0){
                    sb.append(c);
                }
            }
        }



        return sb.toString();
    }
}
